
#include "main.h"
#include "global.h"
#include "init.h"

void RCC_Configuration(void)
{
	SYSCTRL_HSI_Enable(SYSCTRL_HSIOSC_DIV1); // 96M
}

void LEDConfiguration(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_GPIOB_CLK_ENABLE();
	__SYSCTRL_GPIOC_CLK_ENABLE();

	GPIO_InitStruct.IT = GPIO_IT_NONE;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pins = LED_GPIO_PIN;
	GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);

	LEDON;
}

void DR_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_GPIOB_CLK_ENABLE();
	__SYSCTRL_GPIOC_CLK_ENABLE();

	GPIO_InitStruct.IT = GPIO_IT_NONE;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT_PULLUP;
	GPIO_InitStruct.Pins = DR_GPIO_PIN;
	GPIO_Init(DR_GPIO_PORT, &GPIO_InitStruct);
}

void EN_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_GPIOB_CLK_ENABLE();
	__SYSCTRL_GPIOC_CLK_ENABLE();

	GPIO_InitStruct.IT = GPIO_IT_NONE;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT_PULLUP;
	GPIO_InitStruct.Pins = EN_GPIO_PIN;
	GPIO_Init(EN_GPIO_PORT, &GPIO_InitStruct);
}

void FG_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_GPIOB_CLK_ENABLE();
	__SYSCTRL_GPIOC_CLK_ENABLE();

	GPIO_InitStruct.IT = GPIO_IT_NONE;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pins = FG_GPIO_PIN;
	GPIO_Init(FG_GPIO_PORT, &GPIO_InitStruct);
}

void ADC_Configuration(void)
{
	ADC_InitTypeDef ADC_InitStructure = {0};
	__SYSCTRL_ADC_CLK_ENABLE();
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_GPIOB_CLK_ENABLE();

	PA06_ANALOG_ENABLE(); // PA06,AIN6 电流 A
	PA07_ANALOG_ENABLE(); // PA07,AIN7 电流 B
	PB00_ANALOG_ENABLE(); // PB00,AIN8 电流 C
	PB01_ANALOG_ENABLE(); // PB01,AIN9,电位器
	PA08_ANALOG_ENABLE(); // PA08,AIN10电压

	__SYSCTRL_ADC_CLK_ENABLE();

	ADC_InitStructure.ADC_ClkDiv = ADC_Clk_Div2;			  // 48M/(15+33)=1M采样速率
	ADC_InitStructure.ADC_ConvertMode = ADC_ConvertMode_Once; // 单次多通道采样
	ADC_InitStructure.ADC_SQREns = ADC_SqrEns0to4;
	ADC_InitStructure.ADC_IN0.ADC_InputChannel = ADC_InputCH6;
	ADC_InitStructure.ADC_IN0.ADC_SampTime = ADC_SampTime30Clk;
	ADC_InitStructure.ADC_IN1.ADC_InputChannel = ADC_InputCH7;
	ADC_InitStructure.ADC_IN1.ADC_SampTime = ADC_SampTime30Clk;
	ADC_InitStructure.ADC_IN2.ADC_InputChannel = ADC_InputCH8;
	ADC_InitStructure.ADC_IN2.ADC_SampTime = ADC_SampTime30Clk;
	ADC_InitStructure.ADC_IN3.ADC_InputChannel = ADC_InputCH9;
	ADC_InitStructure.ADC_IN3.ADC_SampTime = ADC_SampTime30Clk;
	ADC_InitStructure.ADC_IN4.ADC_InputChannel = ADC_InputCH10;
	ADC_InitStructure.ADC_IN4.ADC_SampTime = ADC_SampTime30Clk;
	ADC_Init(&ADC_InitStructure);

	ADC_ClearITPendingAll();
	ADC_ITConfig(ADC_IT_EOS, ENABLE);
	NVIC_EnableIRQ(ADC_IRQn);
	ADC_ExtTrigCfg(ADC_TRIG_ATIMCC4, ENABLE);

	// ADC使能
	ADC_Enable();
}

void ADC_IRQHandler(void)
{
	/* USER CODE BEGIN */
	if (CW_ADC->ISR_f.EOS)
	{
		ADC_ClearITPendingAll(); // 清除AD所有中断标志

		ADC_GetSqr0Result(&SampleData[0]); //A   计算公式Vo = (61−1.2×IA)/22​(单位：V)
		ADC_GetSqr1Result(&SampleData[1]); //B
		ADC_GetSqr2Result(&SampleData[2]); //C
		ADC_GetSqr3Result(&SampleData[3]); // 电位器
		ADC_GetSqr4Result(&SampleData[4]); // 电压

		if (Motor_Start_F == 1)
		{
			if (bHallStartStep1 == 0 || bHallStartStep1 == 5)
			{
				if (SampleData[1] < DIin)//增益特性‌：输入 IA 每增加 1V，输出下降 1.2/22 ≈ 0.0545V（衰减增益）。
				{
					bb += DIin - SampleData[1];
					bbc++;
					if (bbc >= 30)
					{
						bbc = 0;
						sumb = bb;
						bb = 0;
					}
				}
			} // AB
			else if (bHallStartStep1 == 1 || bHallStartStep1 == 2)
			{
				if (SampleData[2] < DIin)
				{
					cc += DIin - SampleData[2];
					ccc++;
					if (ccc >= 30)
					{
						ccc = 0;
						sumc = cc;
						cc = 0;
					}
				}
			} // AC
			else if (bHallStartStep1 == 3 || bHallStartStep1 == 4)
			{
				if (SampleData[0] < DIin)
				{
					aa += DIin - SampleData[0];
					aac++;
					if (aac >= 30)
					{
						aac = 0;
						suma = aa;
						aa = 0;
					}
				}
			} // BA
		}
		else
		{
			bbc = 0;
			sumb = bb;
			bb = 0;
			ccc = 0;
			sumc = cc;
			cc = 0;
			aac = 0;
			suma = aa;
			aa = 0;
		}

		aavsp += SampleData[3]; // 调速平均滤波累计项
		aacvsp++;
		if (aacvsp >= 200)
		{
			aacvsp = 0;
			sumavsp = aavsp;
			aavsp = 0;
		}
	}
	/* USER CODE END */
}

void BLDC_Configuration(void)
{
	__SYSCTRL_ATIM_CLK_ENABLE();
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_GPIOB_CLK_ENABLE();
	// 上桥引脚
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.IT = GPIO_IT_NONE;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pins = PWM_AP_PIN | PWM_BP_PIN;
	GPIO_Init(CW_GPIOB, &GPIO_InitStruct);
	GPIO_InitStruct.Pins = PWM_CP_PIN;
	GPIO_Init(CW_GPIOB, &GPIO_InitStruct);
	PB05_AFx_ATIMCH1();
	PB06_AFx_ATIMCH2();
	PB07_AFx_ATIMCH3();

	// 下桥引脚
	GPIO_InitStruct.IT = GPIO_IT_NONE;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pins = PWM_AN_PIN;
	GPIO_Init(CW_GPIOA, &GPIO_InitStruct);
	GPIO_InitStruct.Pins = PWM_BN_PIN | PWM_CN_PIN;
	GPIO_Init(CW_GPIOB, &GPIO_InitStruct);

	PWM_AL_ON;
	PWM_BL_ON;
	PWM_CL_ON;

	ATIM_InitTypeDef ATIM_InitStruct;
	ATIM_OCInitTypeDef ATIM_OCInitStruct = {0};
	ATIM_InitStruct.BufferState = ENABLE;							 // 使能缓存寄存器
	ATIM_InitStruct.CounterAlignedMode = ATIM_COUNT_ALIGN_MODE_EDGE; // 边沿对齐
	ATIM_InitStruct.CounterDirection = ATIM_COUNTING_UP;			 // 向上计数；
	ATIM_InitStruct.CounterOPMode = ATIM_OP_MODE_REPETITIVE;		 // 连续运行模式
	ATIM_InitStruct.Prescaler = 1 - 1;								 // 计算时钟 96 MHz
	ATIM_InitStruct.ReloadValue = PWM_PERIOD - 1;					 // PWM_TS = 2399
	ATIM_InitStruct.RepetitionCounter = 0;
	ATIM_Init(&ATIM_InitStruct);

	ATIM_OCInitStruct.BufferState = ENABLE;
	ATIM_OCInitStruct.OCComplement = DISABLE;
	ATIM_OCInitStruct.OCFastMode = DISABLE;
	ATIM_OCInitStruct.OCInterruptState = DISABLE;
	ATIM_OCInitStruct.OCMode = ATIM_OCMODE_PWM1;
	ATIM_OCInitStruct.OCPolarity = ATIM_OCPOLARITY_NONINVERT;
	ATIM_OC1Init(&ATIM_OCInitStruct);
	ATIM_OC2Init(&ATIM_OCInitStruct);
	ATIM_OC3Init(&ATIM_OCInitStruct);
	ATIM_OC4Init(&ATIM_OCInitStruct);

	ATIM_SetCompare1(0);
	ATIM_SetCompare2(0);
	ATIM_SetCompare3(0);
	ATIM_SetCompare4(PWM_PERIOD / 2);

	ATIM_CH1Config(ENABLE);
	ATIM_CH2Config(ENABLE);
	ATIM_CH3Config(ENABLE);
	ATIM_CH4Config(ENABLE);

	ATIM_CtrlPWMOutputs(ENABLE);
	ATIM_Cmd(ENABLE);
}

void BTIM1_Configuration(void) // 1ms进一次中断
{
	BTIM_TimeBaseInitTypeDef BTIM_TimeBaseInitStruct = {0};

	__SYSCTRL_BTIM123_CLK_ENABLE();
	__disable_irq();
	NVIC_EnableIRQ(BTIM1_IRQn);
	__enable_irq();

	BTIM_TimeBaseInitStruct.BTIM_Mode = BTIM_MODE_TIMER;
	BTIM_TimeBaseInitStruct.BTIM_CountMode = ATIM_OP_MODE_REPETITIVE;
	BTIM_TimeBaseInitStruct.BTIM_Period = 1000 - 1;
	BTIM_TimeBaseInitStruct.BTIM_Prescaler = 96 - 1; // 8
	BTIM_TimeBaseInit(CW_BTIM1, &BTIM_TimeBaseInitStruct);

	BTIM_ITConfig(CW_BTIM1, BTIM_IT_UPDATE, ENABLE);
	BTIM_Cmd(CW_BTIM1, ENABLE);
}

void Hall_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	__SYSCTRL_GPIOA_CLK_ENABLE();

	GPIO_InitStruct.IT = GPIO_IT_RISING | GPIO_IT_FALLING;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
	GPIO_Init(CW_GPIOA, &GPIO_InitStruct);

	// 配置中断滤波
	GPIO_ConfigFilter(CW_GPIOA, bv0 | bv2 | bv1, GPIO_FLTCLK_HCLK8);

	GPIOA_INTFLAG_CLR(bv0 | bv1 | bv2);
	NVIC_EnableIRQ(GPIOA_IRQn);
}
